/*
 * Copyright (c) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "hks_import_sign_verify_test.h"
#include "hks_test_adapt_for_de.h"

#include <gtest/gtest.h>
#ifdef L2_STANDARD
#include "file_ex.h"
#endif

using namespace testing::ext;
namespace Unittest::ImportSignVerifyTest {
class HksImportSignVerifyTest : public testing::Test {
public:
    static void SetUpTestCase(void);

    static void TearDownTestCase(void);

    void SetUp();

    void TearDown();
};

void HksImportSignVerifyTest::SetUpTestCase(void)
{
}

void HksImportSignVerifyTest::TearDownTestCase(void)
{
}

void HksImportSignVerifyTest::SetUp()
{
    EXPECT_EQ(HksInitialize(), 0);
}

void HksImportSignVerifyTest::TearDown()
{
    std::system("find /data/service/el1/public/huks_service -user root -delete");
}

static const uint8_t g_xData224[] = {
    0x44, 0x84, 0xdd, 0x24, 0xe3, 0x2a, 0xff, 0x99, 0x5e, 0x2a, 0x48, 0x56, 0xc9, 0x13, 0x16, 0xdf,
    0x78, 0x93, 0x1d, 0x3a, 0xbe, 0xba, 0x24, 0xf3, 0xbb, 0x2e, 0xcd, 0x1f,
};

static const uint8_t g_yData224[] = {
    0x3f, 0x4a, 0x06, 0x7a, 0xb8, 0xc8, 0x8d, 0x5d, 0x60, 0x19, 0x92, 0x29, 0x06, 0x3c, 0x22, 0xdc,
    0x57, 0x32, 0x45, 0xb9, 0xe4, 0x42, 0x9f, 0xf2, 0xf6, 0xb7, 0xf9, 0xbe,
};

static const uint8_t g_zData224[] = {
    0x5b, 0x5b, 0x05, 0xea, 0xec, 0xbf, 0x77, 0xba, 0x29, 0x79, 0x54, 0x5a, 0x4d, 0x10, 0x56, 0x3d,
    0x1c, 0x37, 0x12, 0x4c, 0x34, 0xf7, 0x71, 0xb6, 0x68, 0xf1, 0x26, 0x28,
};

static const uint8_t g_xData256[] = {
    0xa5, 0xb8, 0xa3, 0x78, 0x1d, 0x6d, 0x76, 0xe0, 0xb3, 0xf5, 0x6f, 0x43, 0x9d, 0xcf, 0x60, 0xf6,
    0x0b, 0x3f, 0x64, 0x45, 0xa8, 0x3f, 0x1a, 0x96, 0xf1, 0xa1, 0xa4, 0x5d, 0x3e, 0x2c, 0x3f, 0x13,
};

static const uint8_t g_yData256[] = {
    0xd7, 0x81, 0xf7, 0x2a, 0xb5, 0x8d, 0x19, 0x3d, 0x9b, 0x96, 0xc7, 0x6a, 0x10, 0xf0, 0xaa, 0xbc,
    0x91, 0x6f, 0x4d, 0xa7, 0x09, 0xb3, 0x57, 0x88, 0x19, 0x6f, 0x00, 0x4b, 0xad, 0xee, 0x34, 0x35,
};

static const uint8_t g_zData256[] = {
    0xfb, 0x8b, 0x9f, 0x12, 0xa0, 0x83, 0x19, 0xbe, 0x6a, 0x6f, 0x63, 0x2a, 0x7c, 0x86, 0xba, 0xca,
    0x64, 0x0b, 0x88, 0x96, 0xe2, 0xfa, 0x77, 0xbc, 0x71, 0xe3, 0x0f, 0x0f, 0x9e, 0x3c, 0xe5, 0xf9,
};

static const uint8_t g_xData384[] = {
    0xcf, 0xd3, 0x04, 0xa8, 0xe8, 0x10, 0xf9, 0x95, 0xe3, 0xf3, 0x6c, 0xbb, 0x72, 0x1b, 0x7c, 0x82,
    0x38, 0x67, 0xc7, 0x0f, 0xd5, 0x2e, 0x6d, 0xdf, 0x49, 0xb0, 0xf0, 0x21, 0x2c, 0x70, 0xb7, 0xcf,
    0x21, 0x9d, 0xf3, 0xcd, 0x2e, 0x3e, 0xf9, 0x46, 0x34, 0x8f, 0xc1, 0x77, 0xc7, 0x4d, 0x72, 0x3c,
};

static const uint8_t g_yData384[] = {
    0xb5, 0x87, 0xfd, 0xd1, 0x15, 0xb1, 0x1a, 0x42, 0x9d, 0x72, 0x04, 0xee, 0x29, 0xb6, 0x8d, 0xc0,
    0x18, 0x23, 0xc1, 0x5e, 0x85, 0x61, 0xe9, 0xaf, 0x2c, 0x58, 0x62, 0x77, 0x5d, 0x44, 0xe5, 0x06,
    0x30, 0x67, 0x9c, 0x3d, 0xa8, 0x42, 0xc6, 0x8e, 0x35, 0x5e, 0xc4, 0x67, 0x04, 0xce, 0x88, 0x2d,
};

static const uint8_t g_zData384[] = {
    0xd5, 0x12, 0xd7, 0x12, 0x29, 0x17, 0xe8, 0x27, 0x08, 0xe3, 0x30, 0x42, 0xe5, 0xa4, 0x11, 0xbd,
    0xbd, 0x1b, 0x27, 0xb2, 0xc6, 0x74, 0xe4, 0xbd, 0x5e, 0x2c, 0x75, 0xd1, 0xd8, 0x2b, 0xe7, 0x14,
    0x0c, 0xee, 0x36, 0x24, 0x99, 0x7a, 0x12, 0xeb, 0xac, 0x57, 0x78, 0x76, 0xfb, 0x4b, 0x2e, 0x9e,
};

static const uint8_t g_xData521[] = {
    0x00, 0x50, 0xfb, 0xd2, 0x63, 0x0f, 0xfb, 0xd3, 0x05, 0x22, 0xd4, 0x05, 0x8a, 0x79, 0xab, 0x38,
    0xac, 0x59, 0xe7, 0x20, 0xfb, 0xf3, 0x2f, 0x07, 0xbf, 0xdf, 0x3e, 0x72, 0xe6, 0xef, 0x04, 0x46,
    0x9e, 0xc0, 0x6e, 0x5e, 0x37, 0x04, 0xe5, 0x88, 0x4d, 0xba, 0x54, 0x73, 0x68, 0x43, 0xe4, 0x15,
    0x3e, 0x9a, 0x9c, 0xc4, 0x6e, 0x84, 0xea, 0xb8, 0xa4, 0x85, 0x0c, 0xd6, 0x71, 0x2d, 0x61, 0xca,
    0xe3, 0xe2,
};

static const uint8_t g_yData521[] = {
    0x01, 0xa2, 0x5a, 0x25, 0xa7, 0x4f, 0xe6, 0xfb, 0x81, 0xbf, 0x99, 0x35, 0x0f, 0x6f, 0xf1, 0x92,
    0xa1, 0xbb, 0x2b, 0x2a, 0xfa, 0xb7, 0x8d, 0x36, 0xe2, 0x90, 0xcb, 0xe2, 0xc0, 0x0f, 0xdd, 0x5b,
    0x4d, 0x23, 0x6d, 0x9e, 0x77, 0x61, 0x25, 0x92, 0x72, 0x48, 0xf7, 0x47, 0x35, 0xb2, 0xf6, 0xd6,
    0x24, 0x1d, 0x77, 0xd6, 0x7e, 0x9b, 0x0d, 0x5c, 0x67, 0x54, 0xc6, 0xf4, 0xcf, 0xa5, 0x32, 0x17,
    0x08, 0xc6,
};

static const uint8_t g_zData521[] = {
    0x01, 0xe0, 0x12, 0xfc, 0xbe, 0xe8, 0xc5, 0x35, 0xeb, 0xd0, 0xa8, 0xdd, 0x9a, 0xc4, 0xbe, 0x29,
    0xb4, 0x66, 0x85, 0x24, 0x12, 0x4e, 0xaf, 0x33, 0x8c, 0x29, 0x0f, 0x48, 0x8d, 0x9c, 0x1b, 0x96,
    0xc1, 0x6a, 0x18, 0x12, 0x68, 0x67, 0xf0, 0x43, 0xac, 0x8f, 0x82, 0xa9, 0x86, 0x30, 0xce, 0xa4,
    0x53, 0x71, 0xd6, 0x01, 0x0f, 0x28, 0x1d, 0x8b, 0x31, 0x73, 0xb1, 0xe5, 0x2f, 0xb9, 0x0b, 0xa1,
    0x88, 0x5e,
};

static constexpr uint8_t g_sm2XDataOpenssl1[] = {
    0xa4, 0x3e, 0x4b, 0x83, 0x63, 0x72, 0xfb, 0xc8, 0x08, 0xa8, 0xc1, 0xe7, 0x2b, 0x99, 0xa6, 0x34,
    0x3e, 0xd0, 0x56, 0x66, 0xa9, 0xd0, 0x60, 0x3c, 0x95, 0xcb, 0x76, 0xce, 0x23, 0x70, 0xb6, 0xf6,
};

static constexpr uint8_t g_sm2YDataOpenssl1[] = {
    0x25, 0xd5, 0x4e, 0xdf, 0x8c, 0x2e, 0x1a, 0x55, 0x05, 0x9f, 0x2b, 0xed, 0xaf, 0x43, 0x92, 0x34,
    0x74, 0xd1, 0x4c, 0xee, 0x7e, 0xa6, 0x04, 0xdd, 0x6c, 0x3d, 0x19, 0x20, 0x52, 0xf4, 0xb3, 0x3b,
};

static constexpr uint8_t g_sm2ZDataOpenssl1[] = {
    0x37, 0x92, 0x24, 0xee, 0xef, 0x90, 0x59, 0xdb, 0x37, 0x83, 0x58, 0x66, 0xd9, 0xa0, 0xc1, 0xd4,
    0xfa, 0xfc, 0x18, 0xea, 0x87, 0xfd, 0x55, 0x19, 0xce, 0xf9, 0x27, 0xe9, 0x48, 0x43, 0x68, 0xae,
};

static constexpr uint8_t g_sm2XDataOpenssl3[] = {
    0x57, 0x2F, 0xC1, 0x18, 0x81, 0x54, 0x19, 0xB1, 0x01, 0x33, 0x6B, 0x38, 0xE1, 0xCE, 0xCD, 0x4B,
    0xCE, 0xB9, 0xC0, 0xEB, 0x14, 0x5B, 0x9C, 0xBC, 0x55, 0x0A, 0xCC, 0x9E, 0x14, 0x49, 0x94, 0x96,
};

static constexpr uint8_t g_sm2YDataOpenssl3[] = {
    0xDC, 0xD3, 0xA8, 0x66, 0xBC, 0x8B, 0x5C, 0xAE, 0xA6, 0xE0, 0x06, 0xF3, 0x9F, 0xE8, 0xD5, 0x18,
    0x7C, 0x61, 0xA7, 0xD9, 0xDA, 0x83, 0x9B, 0x70, 0xD3, 0x19, 0x98, 0xBF, 0x79, 0x7C, 0x05, 0x83,
};

static constexpr uint8_t g_sm2ZDataOpenssl3[] = {
    0xDA, 0x06, 0xBE, 0xE8, 0x89, 0x36, 0x92, 0x48, 0xF0, 0x81, 0xB7, 0x44, 0xC3, 0xFC, 0x76, 0x95,
    0xC1, 0xB5, 0x8A, 0x0A, 0xAF, 0x80, 0x49, 0xC7, 0x40, 0x34, 0xD6, 0x8C, 0x6B, 0x25, 0x01, 0xF7,
};

static const uint8_t g_pubData224[] = {
    0x30, 0x4e, 0x30, 0x10, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x05, 0x2b,
    0x81, 0x04, 0x00, 0x21, 0x03, 0x3a, 0x00, 0x04, 0x44, 0x84, 0xdd, 0x24, 0xe3, 0x2a, 0xff, 0x99,
    0x5e, 0x2a, 0x48, 0x56, 0xc9, 0x13, 0x16, 0xdf, 0x78, 0x93, 0x1d, 0x3a, 0xbe, 0xba, 0x24, 0xf3,
    0xbb, 0x2e, 0xcd, 0x1f, 0x3f, 0x4a, 0x06, 0x7a, 0xb8, 0xc8, 0x8d, 0x5d, 0x60, 0x19, 0x92, 0x29,
    0x06, 0x3c, 0x22, 0xdc, 0x57, 0x32, 0x45, 0xb9, 0xe4, 0x42, 0x9f, 0xf2, 0xf6, 0xb7, 0xf9, 0xbe,
};

static const uint8_t g_pubData256[] = {
    0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a,
    0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03, 0x42, 0x00, 0x04, 0xa5, 0xb8, 0xa3, 0x78, 0x1d,
    0x6d, 0x76, 0xe0, 0xb3, 0xf5, 0x6f, 0x43, 0x9d, 0xcf, 0x60, 0xf6, 0x0b, 0x3f, 0x64, 0x45, 0xa8,
    0x3f, 0x1a, 0x96, 0xf1, 0xa1, 0xa4, 0x5d, 0x3e, 0x2c, 0x3f, 0x13, 0xd7, 0x81, 0xf7, 0x2a, 0xb5,
    0x8d, 0x19, 0x3d, 0x9b, 0x96, 0xc7, 0x6a, 0x10, 0xf0, 0xaa, 0xbc, 0x91, 0x6f, 0x4d, 0xa7, 0x09,
    0xb3, 0x57, 0x88, 0x19, 0x6f, 0x00, 0x4b, 0xad, 0xee, 0x34, 0x35,
};

static const uint8_t g_pubData384[] = {
    0x30, 0x76, 0x30, 0x10, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x05, 0x2b,
    0x81, 0x04, 0x00, 0x22, 0x03, 0x62, 0x00, 0x04, 0xcf, 0xd3, 0x04, 0xa8, 0xe8, 0x10, 0xf9, 0x95,
    0xe3, 0xf3, 0x6c, 0xbb, 0x72, 0x1b, 0x7c, 0x82, 0x38, 0x67, 0xc7, 0x0f, 0xd5, 0x2e, 0x6d, 0xdf,
    0x49, 0xb0, 0xf0, 0x21, 0x2c, 0x70, 0xb7, 0xcf, 0x21, 0x9d, 0xf3, 0xcd, 0x2e, 0x3e, 0xf9, 0x46,
    0x34, 0x8f, 0xc1, 0x77, 0xc7, 0x4d, 0x72, 0x3c, 0xb5, 0x87, 0xfd, 0xd1, 0x15, 0xb1, 0x1a, 0x42,
    0x9d, 0x72, 0x04, 0xee, 0x29, 0xb6, 0x8d, 0xc0, 0x18, 0x23, 0xc1, 0x5e, 0x85, 0x61, 0xe9, 0xaf,
    0x2c, 0x58, 0x62, 0x77, 0x5d, 0x44, 0xe5, 0x06, 0x30, 0x67, 0x9c, 0x3d, 0xa8, 0x42, 0xc6, 0x8e,
    0x35, 0x5e, 0xc4, 0x67, 0x04, 0xce, 0x88, 0x2d,
};

static const uint8_t g_pubData521[] = {
    0x30, 0x81, 0x9b, 0x30, 0x10, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x05,
    0x2b, 0x81, 0x04, 0x00, 0x23, 0x03, 0x81, 0x86, 0x00, 0x04, 0x00, 0x50, 0xfb, 0xd2, 0x63, 0x0f,
    0xfb, 0xd3, 0x05, 0x22, 0xd4, 0x05, 0x8a, 0x79, 0xab, 0x38, 0xac, 0x59, 0xe7, 0x20, 0xfb, 0xf3,
    0x2f, 0x07, 0xbf, 0xdf, 0x3e, 0x72, 0xe6, 0xef, 0x04, 0x46, 0x9e, 0xc0, 0x6e, 0x5e, 0x37, 0x04,
    0xe5, 0x88, 0x4d, 0xba, 0x54, 0x73, 0x68, 0x43, 0xe4, 0x15, 0x3e, 0x9a, 0x9c, 0xc4, 0x6e, 0x84,
    0xea, 0xb8, 0xa4, 0x85, 0x0c, 0xd6, 0x71, 0x2d, 0x61, 0xca, 0xe3, 0xe2, 0x01, 0xa2, 0x5a, 0x25,
    0xa7, 0x4f, 0xe6, 0xfb, 0x81, 0xbf, 0x99, 0x35, 0x0f, 0x6f, 0xf1, 0x92, 0xa1, 0xbb, 0x2b, 0x2a,
    0xfa, 0xb7, 0x8d, 0x36, 0xe2, 0x90, 0xcb, 0xe2, 0xc0, 0x0f, 0xdd, 0x5b, 0x4d, 0x23, 0x6d, 0x9e,
    0x77, 0x61, 0x25, 0x92, 0x72, 0x48, 0xf7, 0x47, 0x35, 0xb2, 0xf6, 0xd6, 0x24, 0x1d, 0x77, 0xd6,
    0x7e, 0x9b, 0x0d, 0x5c, 0x67, 0x54, 0xc6, 0xf4, 0xcf, 0xa5, 0x32, 0x17, 0x08, 0xc6,
};

static constexpr uint8_t g_pubSm2DataOpenssl1[] = {
    0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a,
    0x81, 0x1c, 0xcf, 0x55, 0x01, 0x82, 0x2d, 0x03, 0x42, 0x00, 0x04, 0xa4, 0x3e, 0x4b, 0x83, 0x63,
    0x72, 0xfb, 0xc8, 0x08, 0xa8, 0xc1, 0xe7, 0x2b, 0x99, 0xa6, 0x34, 0x3e, 0xd0, 0x56, 0x66, 0xa9,
    0xd0, 0x60, 0x3c, 0x95, 0xcb, 0x76, 0xce, 0x23, 0x70, 0xb6, 0xf6, 0x25, 0xd5, 0x4e, 0xdf, 0x8c,
    0x2e, 0x1a, 0x55, 0x05, 0x9f, 0x2b, 0xed, 0xaf, 0x43, 0x92, 0x34, 0x74, 0xd1, 0x4c, 0xee, 0x7e,
    0xa6, 0x04, 0xdd, 0x6c, 0x3d, 0x19, 0x20, 0x52, 0xf4, 0xb3, 0x3b,
};

static constexpr uint8_t g_pubSm2DataOpenssl3[] = {
    0x30, 0x5A, 0x30, 0x14, 0x06, 0x08, 0x2A, 0x81, 0x1C, 0xCF, 0x55, 0x01, 0x82, 0x2D, 0x06, 0x08,
    0x2A, 0x81, 0x1C, 0xCF, 0x55, 0x01, 0x82, 0x2D, 0x03, 0x42, 0x00, 0x04, 0x57, 0x2F, 0xC1, 0x18,
    0x81, 0x54, 0x19, 0xB1, 0x01, 0x33, 0x6B, 0x38, 0xE1, 0xCE, 0xCD, 0x4B, 0xCE, 0xB9, 0xC0, 0xEB,
    0x14, 0x5B, 0x9C, 0xBC, 0x55, 0x0A, 0xCC, 0x9E, 0x14, 0x49, 0x94, 0x96, 0xDC, 0xD3, 0xA8, 0x66,
    0xBC, 0x8B, 0x5C, 0xAE, 0xA6, 0xE0, 0x06, 0xF3, 0x9F, 0xE8, 0xD5, 0x18, 0x7C, 0x61, 0xA7, 0xD9,
    0xDA, 0x83, 0x9B, 0x70, 0xD3, 0x19, 0x98, 0xBF, 0x79, 0x7C, 0x05, 0x83,
};

static const uint8_t g_ed25519PubData[] = {
    0xab, 0xc7, 0x0f, 0x99, 0x4f, 0x6a, 0x08, 0xd0, 0x9c, 0x5d, 0x10, 0x60, 0xf8, 0x93, 0xd2, 0x8e,
    0xe0, 0x63, 0x0e, 0x70, 0xbf, 0xad, 0x30, 0x41, 0x43, 0x09, 0x27, 0x2d, 0xb3, 0x30, 0x95, 0xa7,
};

static const uint8_t g_ed25519PriData[] = {
    0x61, 0xd3, 0xe7, 0x53, 0x6d, 0x79, 0x5d, 0x71, 0xc2, 0x2a, 0x51, 0x2d, 0x5e, 0xcb, 0x67, 0x3d,
    0xdd, 0xde, 0xf0, 0xac, 0xdb, 0xba, 0x24, 0xfd, 0xf8, 0x3a, 0x7b, 0x32, 0x6e, 0x05, 0xe6, 0x37,
};

static const uint8_t g_x25519PubData[] = {
    0x9c, 0xf6, 0x7a, 0x8d, 0xce, 0xc2, 0x7f, 0xa7, 0xd9, 0xfd, 0xf1, 0xad, 0xac, 0xf0, 0xb3, 0x8c,
    0xe8, 0x16, 0xa2, 0x65, 0xcc, 0x18, 0x55, 0x60, 0xcd, 0x2f, 0xf5, 0xe5, 0x72, 0xc9, 0x3c, 0x54,
};
static const uint8_t g_x25519PriData[] = {
    0x20, 0xd5, 0xbb, 0x54, 0x6f, 0x1f, 0x00, 0x30, 0x4e, 0x33, 0x38, 0xb9, 0x8e, 0x6a, 0xdf, 0xad,
    0x33, 0x6f, 0x51, 0x23, 0xff, 0x4d, 0x95, 0x26, 0xdc, 0xb0, 0x74, 0xb2, 0x5c, 0x7e, 0x85, 0x6c,
};

static const uint8_t g_dsaPubData[] = {
    0x30, 0x82, 0x03, 0x47, 0x30, 0x82, 0x02, 0x39, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x38, 0x04,
    0x01, 0x30, 0x82, 0x02, 0x2c, 0x02, 0x82, 0x01, 0x01, 0x00, 0xa3, 0x45, 0xc9, 0x21, 0x8a, 0xce,
    0x76, 0xf9, 0x52, 0x6e, 0x49, 0x2f, 0x25, 0x05, 0xa9, 0xde, 0xdb, 0xd7, 0x6b, 0x39, 0x59, 0x9c,
    0x73, 0xe4, 0xf0, 0xe9, 0x39, 0xd0, 0xb5, 0x12, 0x3d, 0xc0, 0xb5, 0x2a, 0x08, 0xf3, 0x0a, 0xa4,
    0xfc, 0x4a, 0xdb, 0x00, 0x83, 0x85, 0x05, 0xd2, 0xea, 0x9e, 0x0b, 0xa9, 0x1f, 0x68, 0x77, 0xd6,
    0x13, 0x82, 0xdb, 0xf9, 0x52, 0xe5, 0xae, 0x26, 0x37, 0x2d, 0x8b, 0xbd, 0x2e, 0x1d, 0x9e, 0x0c,
    0xf2, 0xab, 0x1c, 0xe9, 0x78, 0xad, 0x76, 0x16, 0x55, 0x12, 0x81, 0xe0, 0xc9, 0x99, 0x43, 0x9e,
    0xf9, 0x78, 0x31, 0x27, 0x7d, 0x13, 0xd7, 0x05, 0x8c, 0xd7, 0xef, 0x47, 0xc4, 0x9a, 0xc5, 0xa2,
    0x99, 0x2a, 0x6a, 0xa0, 0xd8, 0xc7, 0x26, 0xde, 0xf6, 0xa2, 0x24, 0x12, 0x66, 0x4e, 0x90, 0xa1,
    0xe2, 0x9e, 0xaa, 0xb3, 0x40, 0x93, 0x2a, 0x9d, 0x23, 0x85, 0xec, 0x64, 0xa6, 0x20, 0xdb, 0xe4,
    0xbe, 0x8e, 0xe5, 0x4e, 0xec, 0xf6, 0xa8, 0xca, 0x9b, 0x0d, 0x3e, 0x34, 0x5c, 0x7a, 0xb7, 0x38,
    0x2d, 0x41, 0x48, 0x0c, 0x03, 0xc3, 0x55, 0xfa, 0x10, 0x81, 0x62, 0xc4, 0x08, 0x30, 0xf5, 0x39,
    0x4b, 0x5f, 0x32, 0x22, 0x50, 0x6f, 0x9f, 0xcb, 0xb2, 0x8a, 0x9e, 0x45, 0x71, 0xe2, 0xec, 0xc5,
    0x67, 0xeb, 0x3c, 0xe5, 0x8f, 0x16, 0x44, 0x19, 0xe3, 0x2d, 0xa5, 0x8f, 0xd7, 0xdb, 0x40, 0x3a,
    0x17, 0xa9, 0x0c, 0x19, 0x8b, 0x00, 0x69, 0x22, 0x8d, 0x3f, 0x52, 0x9f, 0x43, 0x06, 0xd5, 0x5d,
    0x79, 0x60, 0xa9, 0xc2, 0xd4, 0x3c, 0x1e, 0x81, 0x05, 0x6e, 0x37, 0x77, 0x82, 0x51, 0x25, 0x74,
    0x6a, 0x99, 0xaa, 0xb0, 0xf2, 0x4f, 0x40, 0x2b, 0x29, 0x3f, 0x34, 0x9c, 0x97, 0x42, 0x63, 0x9c,
    0x49, 0xe8, 0x09, 0x3a, 0xbf, 0x26, 0x8c, 0xcb, 0x7e, 0x11, 0x02, 0x21, 0x00, 0xc4, 0x59, 0x2b,
    0xa5, 0xe1, 0x19, 0x89, 0xf2, 0x2c, 0xde, 0x54, 0x08, 0xd3, 0xfa, 0xd3, 0x37, 0x40, 0xd3, 0xc0,
    0x88, 0xe1, 0x08, 0xf0, 0x06, 0xd4, 0x65, 0x82, 0x46, 0xbe, 0xa1, 0x82, 0xdd, 0x02, 0x82, 0x01,
    0x00, 0x1c, 0x2d, 0x79, 0x2c, 0x1e, 0x9d, 0x9a, 0x69, 0xe3, 0x59, 0xa6, 0xc9, 0xbe, 0xb3, 0x55,
    0x2e, 0x9d, 0xc6, 0xfd, 0x77, 0x33, 0xfe, 0x61, 0x31, 0x6a, 0x2d, 0xcf, 0x12, 0xb9, 0x1f, 0x58,
    0x4a, 0xac, 0xd4, 0xbc, 0xd0, 0xf7, 0xb7, 0x16, 0x3a, 0x6b, 0x80, 0x3e, 0x22, 0x74, 0x81, 0xde,
    0x51, 0x29, 0x95, 0xe6, 0x28, 0x0c, 0xaf, 0x64, 0xba, 0x11, 0x0e, 0x1a, 0xae, 0xb5, 0xb8, 0x40,
    0x59, 0xed, 0x75, 0x5a, 0x5e, 0x54, 0x0b, 0xba, 0x8a, 0xb2, 0x14, 0x23, 0xb0, 0xec, 0x68, 0x18,
    0xa1, 0xd2, 0xfa, 0x2d, 0x16, 0x15, 0xab, 0x1f, 0x0b, 0x18, 0x32, 0x93, 0xc2, 0xd0, 0x54, 0xe0,
    0x37, 0x6f, 0xff, 0x0d, 0x67, 0x4a, 0x90, 0x41, 0x06, 0x33, 0xfc, 0xab, 0xf8, 0xdc, 0x1e, 0x16,
    0xf2, 0x06, 0x93, 0xe3, 0x52, 0x18, 0x46, 0x5e, 0xe3, 0x7f, 0xba, 0x98, 0x56, 0x89, 0x0b, 0xce,
    0x0b, 0xba, 0x01, 0xe2, 0x66, 0x71, 0x85, 0x2a, 0x32, 0x43, 0x9d, 0x48, 0xaf, 0xb9, 0xe4, 0xd3,
    0xc8, 0xdc, 0x3a, 0x8e, 0xb4, 0xf0, 0xa5, 0x11, 0xd4, 0x5f, 0xbf, 0x65, 0x62, 0x76, 0x4c, 0x30,
    0xfb, 0x29, 0x1c, 0x15, 0xa6, 0x16, 0x8a, 0x7f, 0x17, 0x56, 0x40, 0x79, 0x33, 0xd4, 0x91, 0x29,
    0xf1, 0x39, 0x8d, 0xfd, 0x48, 0x97, 0x84, 0xc6, 0x42, 0x1e, 0x83, 0xd2, 0xe5, 0xf9, 0xa4, 0x26,
    0x3c, 0xb8, 0x6a, 0xce, 0x8b, 0xb5, 0x0d, 0xd8, 0x72, 0x38, 0x3b, 0x65, 0xc8, 0x7c, 0x01, 0xf7,
    0x6d, 0x8d, 0x50, 0x87, 0xc2, 0xce, 0x55, 0xfb, 0xe4, 0xf9, 0xe2, 0x98, 0x28, 0x9e, 0x05, 0xdf,
    0x28, 0xcc, 0x0f, 0xe3, 0x54, 0x64, 0x36, 0x2d, 0xa4, 0x7c, 0x5a, 0x0c, 0xcd, 0xe0, 0x51, 0x8f,
    0x38, 0x3a, 0xe7, 0x82, 0x3a, 0x62, 0x69, 0xce, 0xee, 0x53, 0x2a, 0x7c, 0xec, 0x46, 0x5c, 0x51,
    0x33, 0x03, 0x82, 0x01, 0x06, 0x00, 0x02, 0x82, 0x01, 0x01, 0x00, 0x9b, 0xf4, 0x99, 0x33, 0x05,
    0xf0, 0x0c, 0xa9, 0x88, 0xcc, 0xb0, 0x3e, 0x17, 0x6b, 0x67, 0xc7, 0x8e, 0x31, 0x6d, 0xf7, 0x34,
    0xfe, 0xdd, 0x76, 0x8e, 0x3d, 0xfa, 0x8c, 0x39, 0xdd, 0xce, 0xfa, 0xa9, 0xe2, 0x7b, 0xc0, 0x01,
    0x23, 0xf6, 0x62, 0x93, 0x77, 0x69, 0x91, 0x6e, 0xa5, 0x32, 0x46, 0xbb, 0x95, 0x67, 0x81, 0xbd,
    0xe8, 0xb1, 0xe4, 0xdf, 0xc8, 0x75, 0x99, 0x23, 0xc0, 0x29, 0x1b, 0xc7, 0x23, 0x0b, 0xf2, 0x11,
    0xb8, 0x0e, 0x59, 0x25, 0xe3, 0xa2, 0x3f, 0x22, 0xe4, 0xf6, 0x20, 0xd2, 0xdf, 0x58, 0xd2, 0x99,
    0xd8, 0x62, 0x9a, 0x24, 0x2e, 0x13, 0x4b, 0xea, 0xc9, 0x8c, 0xf9, 0x90, 0x6d, 0xa4, 0x34, 0x27,
    0x67, 0xee, 0x5c, 0x10, 0x63, 0x58, 0xc0, 0x96, 0x7b, 0x2d, 0x5b, 0x6c, 0xcd, 0x0c, 0x7b, 0x18,
    0xcc, 0x0d, 0x7d, 0x3a, 0xa7, 0x57, 0x9c, 0x12, 0x10, 0x8a, 0x3a, 0xf4, 0x6b, 0xe4, 0x38, 0xba,
    0x9c, 0xf4, 0x7c, 0x27, 0xeb, 0x06, 0x88, 0x35, 0x6b, 0xd7, 0x01, 0xe3, 0x71, 0x48, 0x41, 0xa4,
    0x1d, 0x9b, 0x1d, 0x06, 0x8a, 0x1f, 0x2c, 0x9d, 0xac, 0x81, 0x50, 0x4d, 0x9b, 0x05, 0xcf, 0xa2,
    0x3f, 0x5c, 0x84, 0x97, 0x82, 0xa6, 0x27, 0x77, 0x88, 0xfa, 0x8a, 0x34, 0x8f, 0x23, 0xcb, 0x79,
    0x51, 0x07, 0xfb, 0x43, 0xab, 0x75, 0xee, 0xd4, 0x0c, 0x6a, 0x4c, 0x5e, 0x1a, 0xc1, 0xa1, 0xd7,
    0x32, 0xf8, 0xbe, 0x2b, 0x82, 0xa1, 0x69, 0x4f, 0x24, 0x25, 0x24, 0x35, 0x5d, 0xf1, 0x4e, 0x07,
    0x0b, 0x0b, 0xb5, 0x79, 0x44, 0xcf, 0xea, 0xb6, 0xfa, 0x99, 0x60, 0x34, 0x5d, 0xad, 0xca, 0x3a,
    0x6c, 0x9e, 0xa1, 0x81, 0xe0, 0x51, 0xc2, 0x10, 0x0e, 0x6c, 0x61, 0x78, 0x07, 0x94, 0x2f, 0xca,
    0x0e, 0x04, 0x48, 0x01, 0xaf, 0x29, 0x17, 0x3e, 0x95, 0x99, 0x2d,
};

static struct HksParam g_importKeyParams[] = {
    { .tag = HKS_TAG_ALGORITHM, .uint32Param = HKS_ALG_DSA },
    { .tag = HKS_TAG_PURPOSE, .uint32Param = HKS_KEY_PURPOSE_SIGN | HKS_KEY_PURPOSE_VERIFY },
    { .tag = HKS_TAG_KEY_SIZE, .uint32Param = HKS_RSA_KEY_SIZE_2048 },
    { .tag = HKS_TAG_DIGEST, .uint32Param = HKS_DIGEST_SHA256 },
    { .tag = HKS_TAG_IMPORT_KEY_TYPE, .uint32Param = HKS_KEY_TYPE_PRIVATE_KEY },
};

static struct HksParam g_initOp1Params[] = {
    { .tag = HKS_TAG_ALGORITHM, .uint32Param = HKS_ALG_DSA },
    { .tag = HKS_TAG_PURPOSE, .uint32Param = HKS_KEY_PURPOSE_SIGN },
    { .tag = HKS_TAG_KEY_SIZE, .uint32Param = HKS_RSA_KEY_SIZE_2048 },
    { .tag = HKS_TAG_DIGEST, .uint32Param = HKS_DIGEST_SHA256 },
};

static struct HksParam g_initOp2Params[] = {
    { .tag = HKS_TAG_ALGORITHM, .uint32Param = HKS_ALG_DSA },
    { .tag = HKS_TAG_PURPOSE, .uint32Param = HKS_KEY_PURPOSE_VERIFY },
    { .tag = HKS_TAG_KEY_SIZE, .uint32Param = HKS_RSA_KEY_SIZE_2048 },
    { .tag = HKS_TAG_DIGEST, .uint32Param = HKS_DIGEST_SHA256 },
};

static struct HksParam g_updateParams[] = {
    { .tag = HKS_TAG_ALGORITHM, .uint32Param = HKS_ALG_DSA },
};

static struct HksParam g_finishParams[] = {
    { .tag = HKS_TAG_ALGORITHM, .uint32Param = HKS_ALG_DSA },
};

static int32_t CopyKey(const uint8_t *key, uint32_t size, struct HksBlob *outKey)
{
    uint8_t *outData = (uint8_t *)HksMalloc(size);
    if (outData == nullptr) {
        return HKS_ERROR_MALLOC_FAIL;
    }

    (void)memcpy_s(outData, size, key, size);
    outKey->data = outData;
    outKey->size = size;

    return HKS_SUCCESS;
}

static int32_t CopyToInner(uint32_t alg, uint32_t importType, struct HksBlob *outKey)
{
    struct HksBlob dataBlob = { 0, nullptr };
    if (importType == HKS_KEY_TYPE_PRIVATE_KEY) {
        dataBlob.data = (alg == HKS_ALG_ED25519) ? (uint8_t *)g_ed25519PriData : (uint8_t *)g_x25519PriData;
        dataBlob.size = (alg == HKS_ALG_ED25519) ? sizeof(g_ed25519PriData) : sizeof(g_x25519PriData);
    } else {
        dataBlob.data = (alg == HKS_ALG_ED25519) ? (uint8_t *)g_ed25519PubData : (uint8_t *)g_x25519PubData;
        dataBlob.size = (alg == HKS_ALG_ED25519) ? sizeof(g_ed25519PubData) : sizeof(g_x25519PubData);
    }

    return CopyKey(dataBlob.data, dataBlob.size, outKey);
}

static int32_t ConstructImportedCurve25519Key(uint32_t alg, uint32_t keySize, uint32_t importType,
    struct HksBlob *outKey)
{
    if ((importType == HKS_KEY_TYPE_PRIVATE_KEY) || (importType == HKS_KEY_TYPE_PUBLIC_KEY)) {
        return CopyToInner(alg, importType, outKey);
    }
    struct HksBlob pubKeyBlob = { 0, nullptr };
    struct HksBlob privKeyBlob = { 0, nullptr };
    privKeyBlob.data = (alg == HKS_ALG_ED25519) ? (uint8_t *)g_ed25519PriData : (uint8_t *)g_x25519PriData;
    privKeyBlob.size = (alg == HKS_ALG_ED25519) ? sizeof(g_ed25519PriData) : sizeof(g_x25519PriData);
    pubKeyBlob.data = (alg == HKS_ALG_ED25519) ? (uint8_t *)g_ed25519PubData : (uint8_t *)g_x25519PubData;
    pubKeyBlob.size = (alg == HKS_ALG_ED25519) ? sizeof(g_ed25519PubData) : sizeof(g_x25519PubData);

    struct HksKeyMaterial25519 material;
    material.keyAlg = (enum HksKeyAlg)alg;
    material.keySize = keySize;
    material.pubKeySize = pubKeyBlob.size;
    material.priKeySize = privKeyBlob.size;
    material.reserved = 0;

    uint32_t size = sizeof(material) + material.pubKeySize + material.priKeySize;
    uint8_t *data = (uint8_t *)HksMalloc(size);
    if (data == nullptr) {
        return HKS_ERROR_MALLOC_FAIL;
    }

    // copy struct material
    if (memcpy_s(data, size, &material, sizeof(material)) != EOK) {
        HKS_FREE(data);
        return HKS_ERROR_BAD_STATE;
    }

    uint32_t offset = sizeof(material);
    // copy publicData
    if (memcpy_s(data + offset, size - offset, pubKeyBlob.data, pubKeyBlob.size) != EOK) {
        HKS_FREE(data);
        return HKS_ERROR_BAD_STATE;
    }
    offset += material.pubKeySize;

    // copy privateData
    if (memcpy_s(data + offset, size - offset, privKeyBlob.data, privKeyBlob.size) != EOK) {
        HKS_FREE(data);
        return HKS_ERROR_BAD_STATE;
    }

    outKey->data = data;
    outKey->size = size;
    return HKS_SUCCESS;
}

static int32_t ConstructImportedDsaPriOrPairKey(uint32_t keySize, uint32_t importType, struct HksBlob *outKey)
{
    bool isPriKey = (importType == HKS_KEY_TYPE_PRIVATE_KEY) ? true : false;
    struct HksKeyMaterialDsa material;
    material.keyAlg = HKS_ALG_DSA;
    material.keySize = keySize;
    material.xSize = sizeof(g_xData);
    material.ySize = isPriKey ? 0 : sizeof(g_yData);
    material.pSize = sizeof(g_pData);
    material.qSize = sizeof(g_qData);
    material.gSize = sizeof(g_gData);

    uint32_t size = sizeof(material) + material.xSize + material.ySize +
        material.pSize + material.qSize + material.gSize;
    uint8_t *data = (uint8_t *)HksMalloc(size);
    if (data == nullptr) {
        return HKS_ERROR_MALLOC_FAIL;
    }

    // copy struct material
    if (memcpy_s(data, size, &material, sizeof(material)) != EOK) {
        HKS_FREE(data);
        return HKS_ERROR_BAD_STATE;
    }

    uint32_t offset = sizeof(material);
    // copy xData
    if (memcpy_s(data + offset, size - offset, g_xData, sizeof(g_xData)) != EOK) {
        HKS_FREE(data);
        return HKS_ERROR_BAD_STATE;
    }

    offset += material.xSize;
    // copy yData
    if (!isPriKey) {
        if (memcpy_s(data + offset, size - offset, g_yData, sizeof(g_yData)) != EOK) {
            HKS_FREE(data);
            return HKS_ERROR_BAD_STATE;
        }
        offset += material.ySize;
    }

    // copy pData
    if (memcpy_s(data + offset, size - offset, g_pData, sizeof(g_pData)) != EOK) {
        HKS_FREE(data);
        return HKS_ERROR_BAD_STATE;
    }
    offset += material.pSize;

    // copy qData
    if (memcpy_s(data + offset, size - offset, g_qData, sizeof(g_qData)) != EOK) {
        HKS_FREE(data);
        return HKS_ERROR_BAD_STATE;
    }
    offset += material.qSize;

    // copy gData
    if (memcpy_s(data + offset, size - offset, g_gData, sizeof(g_gData)) != EOK) {
        HKS_FREE(data);
        return HKS_ERROR_BAD_STATE;
    }

    outKey->data = data;
    outKey->size = size;
    return HKS_SUCCESS;
}

static int32_t ConstructImportedDsaKey(uint32_t keySize, uint32_t importType, struct HksBlob *outKey)
{
    if (importType == HKS_KEY_TYPE_PUBLIC_KEY) {
        return CopyKey(g_dsaPubData, sizeof(g_dsaPubData), outKey);
    } else {
        return ConstructImportedDsaPriOrPairKey(keySize, importType, outKey);
    }
}

static int32_t ConstructEcPrivateKey(const struct HksBlob *zDataBlob, uint32_t keySize, struct HksBlob *outKey)
{
    struct HksKeyMaterialEcc material;
    material.keyAlg = HKS_ALG_ECC;
    material.keySize = keySize;
    material.xSize = 0;
    material.ySize = 0;
    material.zSize = zDataBlob->size;

    uint32_t size = sizeof(material) + material.xSize + material.ySize + material.zSize;
    uint8_t *data = (uint8_t *)HksMalloc(size);
    if (data == nullptr) {
        return HKS_ERROR_MALLOC_FAIL;
    }

    // copy struct material
    if (memcpy_s(data, size, &material, sizeof(material)) != EOK) {
        HKS_FREE(data);
        return HKS_ERROR_BAD_STATE;
    }

    uint32_t offset = sizeof(material);
    // copy zData
    if (memcpy_s(data + offset, size - offset, zDataBlob->data, zDataBlob->size) != EOK) {
        HKS_FREE(data);
        return HKS_ERROR_BAD_STATE;
    }

    outKey->data = data;
    outKey->size = size;
    return HKS_SUCCESS;
}

static int32_t ConstructEccPubKey(uint32_t keySize, struct HksBlob *outKey)
{
    struct HksBlob pubDataBlob = { 0, nullptr };
    switch (keySize) {
        case HKS_ECC_KEY_SIZE_224:
            pubDataBlob.size = sizeof(g_pubData224);
            pubDataBlob.data = (uint8_t *)(g_pubData224);
            break;
        case HKS_ECC_KEY_SIZE_256:
            pubDataBlob.size = sizeof((g_pubData256));
            pubDataBlob.data = (uint8_t *)(g_pubData256);
            break;
        case HKS_ECC_KEY_SIZE_384:
            pubDataBlob.size = sizeof((g_pubData384));
            pubDataBlob.data = (uint8_t *)(g_pubData384);
            break;
        case HKS_ECC_KEY_SIZE_521:
            pubDataBlob.size = sizeof((g_pubData521));
            pubDataBlob.data = (uint8_t *)(g_pubData521);
            break;
        default:
            return HKS_ERROR_INVALID_KEY_SIZE;
    }
    return CopyKey(pubDataBlob.data, pubDataBlob.size, outKey);
}

static int32_t ConstructEcKey(const struct HksBlob *xDataBlob, const struct HksBlob *yDataBlob,
    const struct HksBlob *zDataBlob, uint32_t keySize, struct HksBlob *outKey)
{
    struct HksKeyMaterialEcc material;
    material.keyAlg = HKS_ALG_ECC;
    material.keySize = keySize;
    material.xSize = xDataBlob->size;
    material.ySize = yDataBlob->size;
    material.zSize = zDataBlob->size;

    uint32_t size = sizeof(material) + material.xSize + material.ySize + material.zSize;
    uint8_t *data = (uint8_t *)HksMalloc(size);
    if (data == nullptr) {
        return HKS_ERROR_MALLOC_FAIL;
    }

    // copy struct material
    if (memcpy_s(data, size, &material, sizeof(material)) != EOK) {
        HKS_FREE(data);
        return HKS_ERROR_BAD_STATE;
    }

    uint32_t offset = sizeof(material);
    // copy xData
    if (memcpy_s(data + offset, size - offset, xDataBlob->data, xDataBlob->size) != EOK) {
        HKS_FREE(data);
        return HKS_ERROR_BAD_STATE;
    }

    offset += material.xSize;
    // copy yData
    if (memcpy_s(data + offset, size - offset, yDataBlob->data, yDataBlob->size) != EOK) {
        HKS_FREE(data);
        return HKS_ERROR_BAD_STATE;
    }
    offset += material.ySize;

    // copy zData
    if (memcpy_s(data + offset, size - offset, zDataBlob->data, zDataBlob->size) != EOK) {
        HKS_FREE(data);
        return HKS_ERROR_BAD_STATE;
    }

    outKey->data = data;
    outKey->size = size;
    return HKS_SUCCESS;
}

struct Sm2TestKeyData {
    const uint8_t *pubData;
    const uint32_t pubSize;
    const uint8_t *xData;
    const uint32_t xSize;
    const uint8_t *yData;
    const uint32_t ySize;
    const uint8_t *zData;
    const uint32_t zSize;
};

static int32_t ConstructImportedSm2Key(uint32_t keySize, uint32_t importType, struct HksBlob *outKey,
    Sm2TestKeyData *sm2Key)
{
    if (importType == HKS_KEY_TYPE_PUBLIC_KEY) {
        return CopyKey(sm2Key->pubData, sm2Key->pubSize, outKey);
    }

    bool isPriKey = (importType == HKS_KEY_TYPE_PRIVATE_KEY) ? true : false;
    struct HksKeyMaterialEcc material;
    material.keyAlg = HKS_ALG_SM2;
    material.keySize = keySize;
    material.xSize = isPriKey ? 0 : sm2Key->xSize;
    material.ySize = isPriKey ? 0 : sm2Key->ySize;
    material.zSize = sm2Key->zSize;

    uint32_t size = sizeof(material) + material.xSize + material.ySize + material.zSize;
    uint8_t *data = (uint8_t *)HksMalloc(size);
    if (data == nullptr) {
        return HKS_ERROR_MALLOC_FAIL;
    }

    // copy struct material
    if (memcpy_s(data, size, &material, sizeof(material)) != EOK) {
        HKS_FREE(data);
        return HKS_ERROR_BAD_STATE;
    }

    uint32_t offset = sizeof(material);
    if (!isPriKey) {
        // copy xData
        if (memcpy_s(data + offset, size - offset, sm2Key->xData, sm2Key->xSize) != EOK) {
            HKS_FREE(data);
            return HKS_ERROR_BAD_STATE;
        }

        offset += material.xSize;
        // copy yData
        if (memcpy_s(data + offset, size - offset, sm2Key->yData, sm2Key->ySize) != EOK) {
            HKS_FREE(data);
            return HKS_ERROR_BAD_STATE;
        }
        offset += material.ySize;
    }

    // copy zData
    if (memcpy_s(data + offset, size - offset, sm2Key->zData, sm2Key->zSize) != EOK) {
        HKS_FREE(data);
        return HKS_ERROR_BAD_STATE;
    }

    outKey->data = data;
    outKey->size = size;
    return HKS_SUCCESS;
}

static int32_t ConstructImportedSm2KeyForOpenssl1(uint32_t keySize, uint32_t importType, struct HksBlob *outKey)
{
    struct Sm2TestKeyData keyData = {
        .pubData = g_pubSm2DataOpenssl1,
        .pubSize = sizeof(g_pubSm2DataOpenssl1),
        .xData = g_sm2XDataOpenssl1,
        .xSize = sizeof(g_sm2XDataOpenssl1),
        .yData = g_sm2YDataOpenssl1,
        .ySize = sizeof(g_sm2YDataOpenssl1),
        .zData = g_sm2ZDataOpenssl1,
        .zSize = sizeof(g_sm2ZDataOpenssl1),
    };
    return ConstructImportedSm2Key(keySize, importType, outKey, &keyData);
}

static int32_t ConstructImportedSm2KeyForOpenssl3(uint32_t keySize, uint32_t importType, struct HksBlob *outKey)
{
    struct Sm2TestKeyData keyData = {
        .pubData = g_pubSm2DataOpenssl3,
        .pubSize = sizeof(g_pubSm2DataOpenssl3),
        .xData = g_sm2XDataOpenssl3,
        .xSize = sizeof(g_sm2XDataOpenssl3),
        .yData = g_sm2YDataOpenssl3,
        .ySize = sizeof(g_sm2YDataOpenssl3),
        .zData = g_sm2ZDataOpenssl3,
        .zSize = sizeof(g_sm2ZDataOpenssl3),
    };
    return ConstructImportedSm2Key(keySize, importType, outKey, &keyData);
}

static int32_t ConstructImportedEccKey(uint32_t keySize, uint32_t importType, struct HksBlob *outKey)
{
    if (importType == HKS_KEY_TYPE_PUBLIC_KEY) {
        return ConstructEccPubKey(keySize, outKey);
    }

    struct HksBlob xDataBlob = { 0, nullptr };
    struct HksBlob yDataBlob = { 0, nullptr };
    struct HksBlob zDataBlob = { 0, nullptr };

    switch (keySize) {
        case HKS_ECC_KEY_SIZE_224:
            xDataBlob.size = sizeof(g_xData224);
            xDataBlob.data = (uint8_t *)g_xData224;
            yDataBlob.size = sizeof(g_yData224);
            yDataBlob.data = (uint8_t *)g_yData224;
            zDataBlob.size = sizeof(g_zData224);
            zDataBlob.data = (uint8_t *)g_zData224;
            break;
        case HKS_ECC_KEY_SIZE_256:
            xDataBlob.size = sizeof(g_xData256);
            xDataBlob.data = (uint8_t *)g_xData256;
            yDataBlob.size = sizeof(g_yData256);
            yDataBlob.data = (uint8_t *)g_yData256;
            zDataBlob.size = sizeof(g_zData256);
            zDataBlob.data = (uint8_t *)g_zData256;
            break;
        case HKS_ECC_KEY_SIZE_384:
            xDataBlob.size = sizeof(g_xData384);
            xDataBlob.data = (uint8_t *)g_xData384;
            yDataBlob.size = sizeof(g_yData384);
            yDataBlob.data = (uint8_t *)g_yData384;
            zDataBlob.size = sizeof(g_zData384);
            zDataBlob.data = (uint8_t *)g_zData384;
            break;
        case HKS_ECC_KEY_SIZE_521:
            xDataBlob.size = sizeof(g_xData521);
            xDataBlob.data = (uint8_t *)g_xData521;
            yDataBlob.size = sizeof(g_yData521);
            yDataBlob.data = (uint8_t *)g_yData521;
            zDataBlob.size = sizeof(g_zData521);
            zDataBlob.data = (uint8_t *)g_zData521;
            break;
        default:
            return HKS_ERROR_INVALID_KEY_SIZE;
    }

    if (importType != HKS_KEY_TYPE_PRIVATE_KEY) {
        return ConstructEcKey(&xDataBlob, &yDataBlob, &zDataBlob, keySize, outKey);
    } else {
        return ConstructEcPrivateKey(&zDataBlob, keySize, outKey);
    }
}

static int32_t ConstructImportedKey(uint32_t alg, uint32_t keySize, uint32_t importType, struct HksBlob *outKey,
    bool sm2CompabilityTest)
{
    if (alg == HKS_ALG_ED25519 || alg == HKS_ALG_X25519) {
        return ConstructImportedCurve25519Key(alg, keySize, importType, outKey);
    } else if (alg == HKS_ALG_DSA) {
        return ConstructImportedDsaKey(keySize, importType, outKey);
    } else if (alg == HKS_ALG_ECC) {
        return ConstructImportedEccKey(keySize, importType, outKey);
    } else if (alg == HKS_ALG_SM2) {
        if (sm2CompabilityTest) {
            HKS_LOG_I("compability test for sm2, using key from openssl 1.1.1");
            return ConstructImportedSm2KeyForOpenssl1(keySize, importType, outKey);
        } else {
            HKS_LOG_I("normal test for sm2, using key from openssl 3.0.7");
            return ConstructImportedSm2KeyForOpenssl3(keySize, importType, outKey);
        }
    } else {
        return HKS_ERROR_INVALID_ALGORITHM;
    }
}

static void ModifyImportParams(uint32_t alg, uint32_t purpose, uint32_t keySize, uint32_t digest, uint32_t importType)
{
    g_importKeyParams[TAG_ALG_ID].uint32Param = alg;
    g_importKeyParams[TAG_PURPOSE_ID].uint32Param = purpose;
    g_importKeyParams[TAG_KEY_SIZE_ID].uint32Param = keySize;
    g_importKeyParams[TAG_DIGEST_ID].uint32Param = digest;
    g_importKeyParams[TAG_IMPOT_TYPE_ID].uint32Param = importType;
}

static void ModifyinitOp1Params(uint32_t alg, uint32_t purpose, uint32_t keySize, uint32_t digest)
{
    g_initOp1Params[TAG_ALG_ID].uint32Param = alg;
    g_initOp1Params[TAG_PURPOSE_ID].uint32Param = purpose;
    g_initOp1Params[TAG_KEY_SIZE_ID].uint32Param = keySize;
    g_initOp1Params[TAG_DIGEST_ID].uint32Param = digest;
}

static void ModifyinitOp2Params(uint32_t alg, uint32_t purpose, uint32_t keySize, uint32_t digest)
{
    g_initOp2Params[TAG_ALG_ID].uint32Param = alg;
    g_initOp2Params[TAG_PURPOSE_ID].uint32Param = purpose;
    g_initOp2Params[TAG_KEY_SIZE_ID].uint32Param = keySize;
    g_initOp2Params[TAG_DIGEST_ID].uint32Param = digest;
}

static int32_t ImportKey(const struct HksBlob *keyAlias, const struct HksParam *params, uint32_t paramCount,
    bool sm2CompabilityTest)
{
    struct HksParamSet *paramSet = nullptr;
    int32_t ret = InitParamSet(&paramSet, params, paramCount);
    if (ret != HKS_SUCCESS) {
        return ret;
    }

    struct HksBlob key = { 0, nullptr };

    // The caller guarantees that the access will not cross the border
    ret = ConstructImportedKey(params[TAG_ALG_ID].uint32Param, params[TAG_KEY_SIZE_ID].uint32Param,
        params[TAG_IMPOT_TYPE_ID].uint32Param, &key, sm2CompabilityTest);
    if (ret != HKS_SUCCESS) {
        HksFreeParamSet(&paramSet);
        return ret;
    }

    ret = HksImportKeyForDe(keyAlias, paramSet, &key);
    HKS_FREE(key.data);
    HksFreeParamSet(&paramSet);
    return ret;
}

static int32_t ConstructParamSets(const struct HksParam *initParams, uint32_t initParamCount,
    struct HksParamSet **initParamSet, struct HksParamSet **updateParamSet, struct HksParamSet **finishParamSet)
{
    int32_t ret = InitParamSet(initParamSet, initParams, initParamCount);
    if (ret != HKS_SUCCESS) {
        return ret;
    }

    ret = InitParamSet(updateParamSet, g_updateParams, sizeof(g_updateParams) / sizeof(struct HksParam));
    if (ret != HKS_SUCCESS) {
        HksFreeParamSet(initParamSet);
        return ret;
    }

    ret = InitParamSet(finishParamSet, g_finishParams, sizeof(g_finishParams) / sizeof(struct HksParam));
    if (ret != HKS_SUCCESS) {
        HksFreeParamSet(initParamSet);
        HksFreeParamSet(updateParamSet);
    }
    return ret;
}

static int32_t DoOpDetail(const struct HksBlob *keyAlias, const struct HksParam *initParams, uint32_t initParamCount,
    const struct HksBlob *inData, struct HksBlob *outData)
{
    struct HksParamSet *initParamSet = nullptr;
    struct HksParamSet *updateParamSet = nullptr;
    struct HksParamSet *finishParamSet = nullptr;
    int32_t ret = ConstructParamSets(initParams, initParamCount, &initParamSet, &updateParamSet, &finishParamSet);
    if (ret != HKS_SUCCESS) {
        return ret;
    }

    do {
        uint64_t handleValue = 0;
        struct HksBlob handle = { sizeof(uint64_t), (uint8_t *)&handleValue };
        ret = HksInitForDe(keyAlias, initParamSet, &handle, nullptr);
        if (ret != HKS_SUCCESS) {
            break;
        }

        uint8_t tempBuf[LENGTH_TO_BE_OPERATED] = {0};
        struct HksBlob tmpBlob = { LENGTH_TO_BE_OPERATED, tempBuf };
        ret = HksUpdateForDe(&handle, updateParamSet, inData, &tmpBlob);
        if (ret != HKS_SUCCESS) {
            break;
        }

        struct HksBlob tmpBlob2 = { 0, nullptr };

        // The caller guarantees that the access will not cross the border
        if (initParams[TAG_PURPOSE_ID].uint32Param == HKS_KEY_PURPOSE_VERIFY) {
            ret = HksFinishForDe(&handle, finishParamSet, outData, &tmpBlob2);
        } else {
            ret = HksFinishForDe(&handle, finishParamSet, &tmpBlob2, outData);
        }
    } while (0);

    HksFreeParamSet(&initParamSet);
    HksFreeParamSet(&updateParamSet);
    HksFreeParamSet(&finishParamSet);
    return ret;
}

static int32_t DoOperation(const struct HksBlob *priKeyAlias, const struct HksBlob *pairKeyAlias)
{
    uint8_t *inDataBuf = (uint8_t *)HksMalloc(LENGTH_TO_BE_OPERATED);
    if (inDataBuf == nullptr) {
        return HKS_ERROR_MALLOC_FAIL;
    }

    uint8_t *outDataBuf = (uint8_t *)HksMalloc(LENGTH_MAX);
    if (outDataBuf == nullptr) {
        HKS_FREE(inDataBuf);
        return HKS_ERROR_MALLOC_FAIL;
    }

    struct HksBlob inData = { LENGTH_TO_BE_OPERATED, inDataBuf };
    struct HksBlob outData = { LENGTH_MAX, outDataBuf };
    (void)HksGenerateRandom(nullptr, &inData);

    int32_t ret;
    do {
        /* operation 1: sign */
        ret = DoOpDetail(priKeyAlias, g_initOp1Params, sizeof(g_initOp1Params) / sizeof(struct HksParam),
            &inData, &outData);
        if (ret != HKS_SUCCESS) {
            break;
        }

        /* operation 2: verify */
        ret = DoOpDetail(pairKeyAlias, g_initOp2Params, sizeof(g_initOp2Params) / sizeof(struct HksParam),
            &inData, &outData);
    } while (0);

    HKS_FREE(inDataBuf);
    HKS_FREE(outDataBuf);
    return ret;
}

static void ImportPlainKeyTest(uint32_t alg, uint32_t keySize, uint32_t digest, bool sm2CompabilityTest)
{
    uint32_t purposePri = HKS_KEY_PURPOSE_SIGN;
    uint32_t purposePair = HKS_KEY_PURPOSE_SIGN | HKS_KEY_PURPOSE_VERIFY;
    uint32_t purposePub = HKS_KEY_PURPOSE_VERIFY;

    // import dsa private-key
    uint8_t priAlias[] = "import_dsa_private_key";
    struct HksBlob priKeyAlias = { sizeof(priAlias), priAlias };
    ModifyImportParams(alg, purposePri, keySize, digest, HKS_KEY_TYPE_PRIVATE_KEY);
    int32_t ret = ImportKey(&priKeyAlias, g_importKeyParams, sizeof(g_importKeyParams) / sizeof(struct HksParam),
        sm2CompabilityTest);
    EXPECT_EQ(ret, HKS_SUCCESS) << "import pri key failed";

    // import rsa key-pair
    uint8_t pairAlias[] = "import_dsa_key_pair";
    struct HksBlob pairKeyAlias = { sizeof(pairAlias), pairAlias };
    ModifyImportParams(alg, purposePair, keySize, digest, HKS_KEY_TYPE_KEY_PAIR);
    ret = ImportKey(&pairKeyAlias, g_importKeyParams, sizeof(g_importKeyParams) / sizeof(struct HksParam),
        sm2CompabilityTest);
    EXPECT_EQ(ret, HKS_SUCCESS) << "import key pair failed";

    // import rsa pubKey
    uint8_t pubAlias[] = "import_dsa_public_key";
    struct HksBlob pubKeyAlias = { sizeof(pubAlias), pubAlias };
    ModifyImportParams(alg, purposePub, keySize, digest, HKS_KEY_TYPE_PUBLIC_KEY);
    ret = ImportKey(&pubKeyAlias, g_importKeyParams, sizeof(g_importKeyParams) / sizeof(struct HksParam),
        sm2CompabilityTest);
    EXPECT_EQ(ret, HKS_SUCCESS) << "import key public failed";

    uint32_t purposeOp1 = HKS_KEY_PURPOSE_SIGN;
    uint32_t purposeOp2 = HKS_KEY_PURPOSE_VERIFY;

    ModifyinitOp1Params(alg, purposeOp1, keySize, digest);
    ModifyinitOp2Params(alg, purposeOp2, keySize, digest);

    ret = DoOperation(&priKeyAlias, &pairKeyAlias);
    if (alg == HKS_ALG_SM2 && digest == HKS_DIGEST_SM3) {
        EXPECT_EQ(ret, HKS_ERROR_INVALID_ARGUMENT) << "operation failed";
    } else {
        EXPECT_EQ(ret, HKS_SUCCESS) << "operation failed";
    }

    ret = DoOperation(&priKeyAlias, &pubKeyAlias);
    if (alg == HKS_ALG_SM2 && digest == HKS_DIGEST_SM3) {
        EXPECT_EQ(ret, HKS_ERROR_INVALID_ARGUMENT) << "operation 2 failed";
    } else {
        EXPECT_EQ(ret, HKS_SUCCESS) << "operation 2 failed";
    }

    // delete keys
    (void)HksDeleteKeyForDe(&priKeyAlias, nullptr);
    (void)HksDeleteKeyForDe(&pairKeyAlias, nullptr);
    (void)HksDeleteKeyForDe(&pubKeyAlias, nullptr);
}

static void ImportInvalidPurposePlainKeyTest(uint32_t alg, uint32_t keySize, uint32_t digest, uint32_t purpose,
    bool sm2CompabilityTest)
{
    // import private-key
    uint8_t priAlias[] = "import_private_key_1";
    struct HksBlob priKeyAlias = { sizeof(priAlias), priAlias };
    ModifyImportParams(alg, purpose, keySize, digest, HKS_KEY_TYPE_PRIVATE_KEY);
    int32_t ret = ImportKey(&priKeyAlias, g_importKeyParams, sizeof(g_importKeyParams) / sizeof(struct HksParam),
        sm2CompabilityTest);
    EXPECT_EQ(ret, HKS_ERROR_INVALID_PURPOSE) << "import pri key failed";

    // import key-pair
    uint8_t pairAlias[] = "import_key_pair_1";
    struct HksBlob pairKeyAlias = { sizeof(pairAlias), pairAlias };
    ModifyImportParams(alg, purpose, keySize, digest, HKS_KEY_TYPE_KEY_PAIR);
    ret = ImportKey(&pairKeyAlias, g_importKeyParams, sizeof(g_importKeyParams) / sizeof(struct HksParam),
        sm2CompabilityTest);
    EXPECT_EQ(ret, HKS_ERROR_INVALID_PURPOSE) << "import key pair failed";
}
#ifdef _USE_OPENSSL_
/**
 * @tc.name: HksImportSignVerifyTest.HksImportSignVerifyTest001
 * @tc.desc: import dsa 2048-sign/verify-sha256
 * @tc.type: FUNC normal
 */
HWTEST_F(HksImportSignVerifyTest, HksImportSignVerifyTest001, TestSize.Level0)
{
    ImportPlainKeyTest(HKS_ALG_DSA, HKS_RSA_KEY_SIZE_2048, HKS_DIGEST_SHA256, false);
}

/**
 * @tc.name: HksImportSignVerifyTest.HksImportSignVerifyTest002
 * @tc.desc: import ed25519 256-sign/verify
 * @tc.type: FUNC normal
 */
HWTEST_F(HksImportSignVerifyTest, HksImportSignVerifyTest002, TestSize.Level0)
{
    ImportPlainKeyTest(HKS_ALG_ED25519, HKS_CURVE25519_KEY_SIZE_256, HKS_DIGEST_SHA256, false);
}
#endif

/**
 * @tc.name: HksImportSignVerifyTest.HksImportSignVerifyTest003
 * @tc.desc: import ecc-p224
 * @tc.type: FUNC normal
 */
HWTEST_F(HksImportSignVerifyTest, HksImportSignVerifyTest003, TestSize.Level0)
{
    ImportPlainKeyTest(HKS_ALG_ECC, HKS_ECC_KEY_SIZE_224, HKS_DIGEST_SHA256, false);
}

/**
 * @tc.name: HksImportSignVerifyTest.HksImportSignVerifyTest004
 * @tc.desc: import ecc-p256
 * @tc.type: FUNC normal
 */
HWTEST_F(HksImportSignVerifyTest, HksImportSignVerifyTest004, TestSize.Level0)
{
    ImportPlainKeyTest(HKS_ALG_ECC, HKS_ECC_KEY_SIZE_256, HKS_DIGEST_SHA384, false);
}

/**
 * @tc.name: HksImportSignVerifyTest.HksImportSignVerifyTest005
 * @tc.desc: import ecc-p384
 * @tc.type: FUNC normal
 */
HWTEST_F(HksImportSignVerifyTest, HksImportSignVerifyTest005, TestSize.Level0)
{
    ImportPlainKeyTest(HKS_ALG_ECC, HKS_ECC_KEY_SIZE_384, HKS_DIGEST_SHA512, false);
}

/**
 * @tc.name: HksImportSignVerifyTest.HksImportSignVerifyTest006
 * @tc.desc: import ecc-p521
 * @tc.type: FUNC normal
 */
HWTEST_F(HksImportSignVerifyTest, HksImportSignVerifyTest006, TestSize.Level0)
{
    ImportPlainKeyTest(HKS_ALG_ECC, HKS_ECC_KEY_SIZE_521, HKS_DIGEST_SHA224, false);
}

#ifdef _USE_OPENSSL_
/**
 * @tc.name: HksImportSignVerifyTest.HksImportSignVerifyTest007
 * @tc.desc: import SM2-256
 * @tc.type: FUNC normal
 */
HWTEST_F(HksImportSignVerifyTest, HksImportSignVerifyTest007, TestSize.Level0)
{
    ImportPlainKeyTest(HKS_ALG_SM2, HKS_SM2_KEY_SIZE_256, HKS_DIGEST_SM3, false);
}
#endif

/**
 * @tc.name: HksImportSignVerifyTest.HksImportSignVerifyTest008
 * @tc.desc: import ecc-p256 with unwrap purpose
 * @tc.type: FUNC normal
 * @tc.require:issueI611S5
 */
HWTEST_F(HksImportSignVerifyTest, HksImportSignVerifyTest008, TestSize.Level0)
{
    ImportInvalidPurposePlainKeyTest(HKS_ALG_ECC, HKS_ECC_KEY_SIZE_256, HKS_DIGEST_NONE, HKS_KEY_PURPOSE_UNWRAP, false);
}

/**
 * @tc.name: HksImportSignVerifyTest.HksImportSignVerifyTest009
 * @tc.desc: import x25519 with unwrap purpose
 * @tc.type: FUNC normal
 * @tc.require:issueI611S5
 */
HWTEST_F(HksImportSignVerifyTest, HksImportSignVerifyTest009, TestSize.Level0)
{
    ImportInvalidPurposePlainKeyTest(HKS_ALG_X25519, HKS_CURVE25519_KEY_SIZE_256, HKS_DIGEST_SHA256,
        HKS_KEY_PURPOSE_UNWRAP, false);
}

#ifdef _USE_OPENSSL_
/**
 * @tc.name: HksImportSignVerifyTest.HksImportSignVerifyTest010
 * @tc.desc: import SM2-256
 * @tc.type: FUNC normal, compatibility test for openssl 1.1.1 and 3.0.7
 */
HWTEST_F(HksImportSignVerifyTest, HksImportSignVerifyTest010, TestSize.Level0)
{
    ImportPlainKeyTest(HKS_ALG_SM2, HKS_SM2_KEY_SIZE_256, HKS_DIGEST_SM3, true);
}
#endif
} // namespace Unittest::ImportSignVerifyTest
